import 'package:appflutterhomeschool/utils/utils.dart';
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

///用户： ninetailedfox
///时间： 2020/7/24
///邮箱： 1037438704@qq.com
///功能： 数据统计组件
class DataStatisticsWidget extends StatefulWidget {
  @override
  _DataStatisticsWidgetState createState() => _DataStatisticsWidgetState();
}

class _DataStatisticsWidgetState extends State<DataStatisticsWidget> {
  int touchedIndex;

  @override
  Widget build(BuildContext context) {
    List list = [
      "wxa",
      "wxb",
      "wxc",
      "wxd",
    ];
    return ListView.builder(
        physics: NeverScrollableScrollPhysics(),
        shrinkWrap: true,
        itemCount: list.length,
        itemBuilder: (context, index) {
          return Image.asset(Utils.getImgPath(list[index]));
        });

//    return Container(
//      color: Colors.white,
//      child: Column(
//        children: [
//
//
////           _textWidget("1,你的性别"),
////          pieChartSample(),
////          _textWidget("2,年龄"),
////          pieChartSample(),
//        ],
//      ),
//    );
  }

  Widget _textWidget(String title) {
    return Container(
      padding: EdgeInsets.all(ScreenUtil().setWidth(34)),
      child: Text(title ?? ""),
      alignment: Alignment.centerLeft,
    );
  }

  Widget pieChartSample() {
    return AspectRatio(
      aspectRatio: 1.8,
      child: Card(
        color: Colors.white,
        child: Column(
          children: [
            Expanded(
              child: AspectRatio(
                aspectRatio: 1.9,
                child: PieChart(
                  PieChartData(
                      pieTouchData:
                          PieTouchData(touchCallback: (pieTouchResponse) {
                        setState(() {
                          if (pieTouchResponse.touchInput is FlLongPressEnd ||
                              pieTouchResponse.touchInput is FlPanEnd) {
                            touchedIndex = -1;
                          } else {
                            touchedIndex = pieTouchResponse.touchedSectionIndex;
                          }
                        });
                      }),
                      startDegreeOffset: 180,
                      borderData: FlBorderData(
                        show: false,
                      ),
                      sectionsSpace: 12,
                      centerSpaceRadius: 0,
                      sections: showingSections()),
                ),
              ),
            ),
            Row(
              children: [
                Spacer(),
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                      color: Colors.black,
                      borderRadius: BorderRadius.circular(5)),
                ),
                Text("男"),
                Spacer(),
                Container(
                  height: 10,
                  width: 10,
                  margin: EdgeInsets.only(right: 5),
                  decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.circular(5)),
                ),
                Text("女"),
                Spacer(),
              ],
            ),
            SizedBox(height: 5)
          ],
        ),
      ),
    );
  }

  List<PieChartSectionData> showingSections() {
    return List.generate(
      2,
      (i) {
        final isTouched = i == touchedIndex;
        final double opacity = isTouched ? 1 : 0.6;
        switch (i) {
          case 0:
            return PieChartSectionData(
              color: const Color(0x00000000).withOpacity(opacity),
              value: 2 / 4,
              title: '',
              radius: 80,
              titleStyle: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: const Color(0xff044d7c)),
              titlePositionPercentageOffset: 0.55,
            );
          case 1:
            return PieChartSectionData(
              color: const Color(0xff0293ee).withOpacity(opacity),
              value: 3 / 4,
              title: '',
              radius: 65,
              titleStyle: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: const Color(0xff90672d)),
              titlePositionPercentageOffset: 0.55,
            );
          case 2:
            return PieChartSectionData(
              color: const Color(0xff845bef).withOpacity(opacity),
              value: 25,
              title: '',
              radius: 60,
              titleStyle: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: const Color(0xff4c3788)),
              titlePositionPercentageOffset: 0.6,
            );
          case 3:
            return PieChartSectionData(
              color: const Color(0xff13d38e).withOpacity(opacity),
              value: 25,
              title: '',
              radius: 70,
              titleStyle: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                  color: const Color(0xff0c7f55)),
              titlePositionPercentageOffset: 0.55,
            );
          default:
            return null;
        }
      },
    );
  }
}
